package leetcode.string;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangmaolin1
 * @date 2021/12/12 上午10:02
 */
public class _567字符串的排列 {
    /**
     * 滑动窗口解决 给你两个字符串 s1 和 s2 ，写一个函数来判断 s2 是否包含 s1 的排列。如果是，返回 true ；否则，返回 false 。
     * <p>
     * 换句话说，s1 的排列之一是 s2 的 子串 。
     * <p>
     * <p>
     * 输入：s1 = "ab" s2 = "eidbaooo" 输出：true 解释：s2 包含 s1 的排列之一 ("ba").
     * <p>
     * 来源：力扣（LeetCode） 链接：https://leetcode-cn.com/problems/permutation-in-string 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
     *
     * @param s1
     * @param s2
     * @return
     */
    public static boolean checkInclusion(String s1, String s2) {
        if (s1 == null || s1.length() <= 0) {
            return false;
        }
        if (s2 == null || s2.length() == 0) {
            return false;
        }
        if (s2.length() < s1.length()) {
            return false;
        }
        Map<Character, Integer> totalMap = new HashMap<>();
        Map<Character, Integer> countMap = new HashMap<>();
        for (int i = 0; i < s1.length(); i++) {
            char c = s1.charAt(i);
            Integer count = totalMap.get(c);
            totalMap.put(c, count == null ? 1 : ++count);
            countMap.put(c, 0);
        }
        int start = 0;
        int end = s1.length() - 1;
        while (end < s2.length()) {
            int l = start;
            while (l <= end) {
                char c = s2.charAt(l);
                if (totalMap.containsKey(c)) {
                    Integer count = countMap.get(c);
                    Integer totalCount = totalMap.get(c);
                    if (count.equals(totalCount)) {
                        int last = end - l + 1;
                        if ((l + last) < s2.length()) {

                        } else {
                            return false;
                        }
                        start++;
                        end++;
                        countMap.put(c, --count);
                        break;
                    } else {
                        ++count;
                        countMap.put(c, count);
                        if (count.equals(totalCount) && l == end) {
                            return true;
                        }
                        l++;
                    }
                } else {
                    start++;
                    end++;
                    break;
                }
            }

        }
        return false;
    }

    public static boolean checkInclusion2(String s1, String s2) {
        char[] pattern = s1.toCharArray();
        char[] text = s2.toCharArray();

        int pLen = s1.length();
        int tLen = s2.length();

        int[] pFreq = new int[26];
        int[] winQ = new int[26];

        for (int i = 0; i < pLen; i++) {
            pFreq[pattern[i] - 'a']++;
        }
        int pCount = 0;
        for (int i = 0; i < 26; i++) {
            if (pFreq[i] > 0) {
                pCount++;
            }
        }

        int left = 0;
        int right = 0;

        int winCount = 0;
        while (right < tLen) {
            if (pFreq[text[right] - 'a'] > 0) {
                winQ[text[right] - 'a']++;
                if (winQ[text[right]-'a']==pFreq[text[right]-'a']){
                    winCount++;
                }
            }
            right++;
            while (pCount==winCount){
                if (right-left==pLen){
                    return true;
                }
                if (pFreq[text[left] - 'a'] > 0) {
                    winQ[text[left] - 'a']--;
                    if (winQ[text[left]-'a']<pFreq[text[left]-'a']){
                        winCount--;
                    }
                }
                left++;
            }

        }
        return false;
    }

    /**
     * 滑动窗口
     * 这个好理解
     * @param s1
     * @param s2
     * @return
     */
    public static boolean checkInclusion3(String s1, String s2) {
        int n = s1.length(), m = s2.length();
        if (n > m) {
            return false;
        }
        int[] cnt1 = new int[26];
        int[] cnt2 = new int[26];
        for (int i = 0; i < n; ++i) {
            ++cnt1[s1.charAt(i) - 'a'];
            ++cnt2[s2.charAt(i) - 'a'];
        }
        if (Arrays.equals(cnt1, cnt2)) {
            return true;
        }
        for (int i = n; i < m; ++i) {
            //向右滑动，新进来窗口的元素
            ++cnt2[s2.charAt(i) - 'a'];
            //左边出窗口的元素
            --cnt2[s2.charAt(i - n) - 'a'];
            if (Arrays.equals(cnt1, cnt2)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 简化3
     * @param s1
     * @param s2
     * @return
     */
    public static boolean checkInclusion4(String s1, String s2) {
        int n = s1.length(), m = s2.length();
        if (n > m) {
            return false;
        }
        int[] cnt = new int[26];
        for (int i = 0; i < n; ++i) {
            --cnt[s1.charAt(i) - 'a'];
            ++cnt[s2.charAt(i) - 'a'];
        }
        int diff=0;
        for (int i : cnt) {
            if (i!=0){
                diff++;
            }
        }
        if (diff==0){
            return true;
        }
        for (int i = n; i < m; ++i) {
            int inItem = s2.charAt(i) - 'a';
            int outItem = s2.charAt(i - n) - 'a';
            if (inItem==outItem){
                continue;
            }
            if (cnt[inItem]==0){
                //之前该元素没有不同的，则此次就要+1diff
                diff++;
            }
            //对应元素加1
            cnt[inItem]++;
            if (cnt[inItem]==0){
                //加1后如果=0了，说明之前差一个，现在元素相同了，则 diff-1
                diff--;
            }
            if (cnt[outItem]==0){
                diff++;
//                new ArrayList<>().addAll();
            }
            cnt[outItem]--;
            if (cnt[outItem]==0){
                diff--;
            }
            if (diff==0){
                return true;
            }

        }
        return false;
    }
    public boolean checkInclusio44n(String s1, String s2) {
        Map<Character,Integer>need=new HashMap<>();
        Map<Character,Integer>window=new HashMap<>();

        for(Character c :s1.toCharArray()){
            need.put(c,need.getOrDefault(c,0)+1);
        }
        int left=0;
        int right=0;
        int valid = 0;
        while (right < s2.length()){
            char c = s2.charAt(right);
            right ++;
            if (need.get(c)!=null){
                window.put(c,window.getOrDefault(c,0)+1);
                if(window.get(c) == need.get(c)){
                    valid ++;
                }
            }
            while((right - left)>=s1.length()){
                if(valid == need.size()){
                    return true;
                }
                char d = s2.charAt(left);
                left ++;
                if(need.get(d)!=null){
                    if(window.get(d).equals(need.get(d))){
                        valid--;
                    }
                    window.put(d,window.getOrDefault(d,0)-1);
                }
            }

        }
        return false;
    }

    public static boolean checkInclusion5(String t, String s) {
        HashMap<Character, Integer> need = new HashMap<>();
        HashMap<Character, Integer> window = new HashMap<>();
        for (int i = 0; i < t.length(); i++) {
            char c = t.charAt(i);
            need.put(c, need.getOrDefault(c, 0) + 1);
        }

        int left = 0, right = 0;
        int valid = 0;
        while (right < s.length()) {
            char c = s.charAt(right);
            right++;
            if (need.containsKey(c)) {
                window.put(c, window.getOrDefault(c, 0) + 1);
                if (window.get(c).equals(need.get(c)))
                    valid++;
            }

            // 判断左侧窗口是否要收缩
            while (right - left >= t.length()) {
                // 在这里判断是否找到了合法的子串
                if (valid == need.size())
                    return true;
                char d = s.charAt(left);
                left++;
                if (need.containsKey(d)) {
                    if (window.get(d).equals(need.get(d)))
                        valid--;
                    window.put(d, window.getOrDefault(d, 0) - 1);
                }
            }
        }
        return false;
    }
    public static void main(String[] args) {
        //"ab"
        //"eidbaooo"
        System.out.println(checkInclusion5("ab", "eidbaooo"));
    }
}
